package com.talent.jiwen.teacher;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.talent.jiwen.R;
import com.talent.jiwen.base.BaseActivity;
import com.talent.jiwen.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen.http.common.ApiFactory;
import com.talent.jiwen.http.common.HttpUtil;
import com.talent.jiwen.http.common.ProgressSubscriber;
import com.talent.jiwen.http.result.EvaluateListResult;
import com.talent.jiwen.ui.widgets.StarBar;
import com.talent.jiwen.util.DateUtils;
import com.talent.jiwen.util.ImageUtil;
import com.talent.jiwen.util.SPConstant;
import com.talent.jiwen.util.SPUtil;
import com.talent.jiwen.util.Validators;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.Observable;

public class TeacherEvaluateActivity extends BaseActivity {

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;

    private List<EvaluateListResult.Evaluate> evaluateList = new ArrayList<>();

    private String teacherId;
    private int startIndex = 1;

    private static final String PARAM_TEAHCER_ID = "param.teacher.id";

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_teacher_evaluate;
    }

    @Override
    public String titleName() {
        return "教师评价";
    }

    @Override
    protected void initData() {

        teacherId = getIntent().getStringExtra(PARAM_TEAHCER_ID);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recycleView.setLayoutManager(layoutManager);
        recycleView.setAdapter(new CommonAdapter<EvaluateListResult.Evaluate>(this , R.layout.item_teacher_evaluate , evaluateList){

            @Override
            protected void convert(ViewHolder holder, EvaluateListResult.Evaluate evaluate, int position) {
                ImageView studentHeadIv = holder.getView(R.id.studentHeadIv);
                ImageUtil.loadCircleImage(TeacherEvaluateActivity.this , evaluate.getStudentHeadImage() , studentHeadIv);
                holder.setText(R.id.studentNameTv , evaluate.getStudentName());
                StarBar effectRatingBar = holder.getView(R.id.effectRatingBar);
                StarBar serverRatingBar = holder.getView(R.id.serverRatingBar);
                LinearLayout starLayout = holder.getView(R.id.starLayout);
                LinearLayout solveLayout = holder.getView(R.id.solveLayout);

                if (evaluate.getSolveStatus() == 1){
                    starLayout.setVisibility(View.VISIBLE);
                    solveLayout.setVisibility(View.GONE);

                    effectRatingBar.setIntegerMark(false);
                    effectRatingBar.setCanTeach(false);
                    effectRatingBar.setStarMark(Float.parseFloat(evaluate.getEffectScore()));

                    serverRatingBar.setIntegerMark(false);
                    serverRatingBar.setCanTeach(false);
                    serverRatingBar.setStarMark(Float.parseFloat(evaluate.getServeScore()));
                }else{
                    starLayout.setVisibility(View.GONE);
                    solveLayout.setVisibility(View.VISIBLE);
                }

                holder.setText(R.id.timeTv , DateUtils.ms2Date1(evaluate.getCreateTime()));
                holder.setText(R.id.contentTv ,  evaluate.getEvaluationMessage());

                View divider = holder.getView(R.id.divider);

                if (position == evaluateList.size() - 1){
                    divider.setVisibility(View.GONE);
                }else{
                    divider.setVisibility(View.VISIBLE);
                }
            }
        });

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                startIndex = 1;
                getTeacherEvaluationList();
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                startIndex ++;
                getTeacherEvaluationList();
            }
        });

        refreshLayout.autoRefresh();
    }

    /**获取老师的评价列表
     *
     */
    private void getTeacherEvaluationList(){
        Map<String, String> map = new HashMap<>();
        map.put("teacherId", teacherId);
        map.put("startIndex" , startIndex + "");

        Observable observable = ApiFactory.getEducationApiSingleton().getTeacherEvaluationList(SPUtil.stringOut(SPConstant.TOKEN), map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<EvaluateListResult>(TeacherEvaluateActivity.this) {
            @Override
            protected void _onNext(EvaluateListResult result) {
                if (startIndex == 1){
                    evaluateList.clear();
                    evaluateList.addAll(result.getEvaluationList());
                    recycleView.getAdapter().notifyDataSetChanged();
                    refreshLayout.finishRefresh();
                }else{
                    evaluateList.addAll(result.getEvaluationList());
                    recycleView.getAdapter().notifyDataSetChanged();
                    refreshLayout.finishLoadMore();

                    if (evaluateList.size() < 10){
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
                if (null != refreshLayout){
                    refreshLayout.finishRefresh();
                }
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject, false);
    }

    /**启动该页面
     *
     * @param context
     * @param teacherId
     */
    public static void startTeahcerEvaluateActivity(Context context , String teacherId){
        Intent intent = new Intent();
        intent.setClass(context , TeacherEvaluateActivity.class);
        intent.putExtra(PARAM_TEAHCER_ID , teacherId);
        context.startActivity(intent);
    }
}
